package com.esms.controller;

import com.esms.dao.EmployeeMapper;
import com.esms.po.Bonus;
import com.esms.po.Employee;
import com.esms.po.Fassessment;
import com.esms.po.Gassessment;
import com.esms.po.Grand;
import com.esms.po.Satisfaction;
import com.esms.service.ILogService;
import com.esms.service.impl.EmployeeBonusServerImpl;
import com.esms.service.impl.EmployeeSatisfactionServerImpl;
import com.esms.utils.DateUtil;
import com.esms.vo.EmployeeBonusVO;
import com.esms.vo.EmployeeSatisfactionVO;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author 帅帅帅帅
 *
 */

@Controller
public class EmployeeSatisfactionController {

	@Autowired
	ILogService logService;
	
	@Autowired
	private EmployeeSatisfactionServerImpl EmployeeSatisfactionService;
	
	@Autowired
    private EmployeeMapper employeeMapper = null;
	
    @RequestMapping("employeeSatisfaction.do")
    @ResponseBody
    public EmployeeSatisfactionVO findSelectiveSatisfaction(
    		@RequestParam(value="d_id", defaultValue="0") int fdid,
            @RequestParam(value="e_id", defaultValue="0") String fEid,
			@RequestParam(value="e_name", defaultValue="0") String eName,
			HttpSession httpSession) throws Exception {

		Integer eId = (Integer) httpSession.getAttribute("employeeId");
		// pageNum:起始页面 pageSize:每页的大小
		// PageHelper.startPage(pageNum,limit);
		// 查找条件，一定要紧跟在startPage后
	    List<Satisfaction> satisfactionList = new ArrayList<Satisfaction>();
	    try{
			String quarter = DateUtil.dateTimeDeal();
		    Employee employee = employeeMapper.selectByPrimaryKey(eId);
		    String e_account = employee.geteAccount();
		    List<String> accountDBList = EmployeeSatisfactionService.getDBaccount();
			for(int i=0;i<accountDBList.size();i++) {
				String accountDB = accountDBList.get(i);
				if (e_account.equals(accountDB)) {
					satisfactionList = EmployeeSatisfactionService.getEmployeeSatisfactionList(e_account, fdid, fEid, eName,quarter);
				}
			}
	    }catch (Exception e){
	    	e.printStackTrace();
	    }

		// 设置前台需要的数据
	    EmployeeSatisfactionVO employeeSatisfactionVO = new EmployeeSatisfactionVO();
	    employeeSatisfactionVO.setCode(0);
	    employeeSatisfactionVO.setMsg("");
	    employeeSatisfactionVO.setCount(satisfactionList.size());
	    employeeSatisfactionVO.setData(satisfactionList);
		return employeeSatisfactionVO;
	}
    
    @RequestMapping("updateSatisfaction.do")
    @ResponseBody
    public EmployeeSatisfactionVO updateBonus(
    		@RequestParam("list") String list,
            HttpSession httpSession) throws Exception {
    	int req = 0;
    	String reMsg;
    	ObjectMapper mapper = new ObjectMapper();
    	JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, Satisfaction.class);
    	List<Satisfaction> listSatisfaction =  (List<Satisfaction>)mapper.readValue(list, jt);
    	/*我的工号查询*/
    	Integer eId = (Integer) httpSession.getAttribute("employeeId");
    	Employee employee = employeeMapper.selectByPrimaryKey(eId);
    	reMsg = "评价成功";
//		logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
//				"满意度测评成功");
    	//获取系统当前时间
		String quarter = DateUtil.dateTimeDeal();
        String systemTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));

        ArrayList<Gassessment> gassessList = new ArrayList<Gassessment>();

    	for(int i=0;i<listSatisfaction.size();i++) {
    		Satisfaction grandSatisfaction = listSatisfaction.get(i);
    		/*输入分数判断*/
			Gassessment gassessment = new Gassessment();
    		/*设置同级评分表*/
    		gassessment.setgAuserid(grandSatisfaction.getDid());
    		gassessment.setgAccount(employee.geteAccount());
    		gassessment.setgDuserid(grandSatisfaction.geteAccount());
    		gassessment.setgCreateTime(systemTime);
    		gassessment.setgCreateQuarter(quarter);
    		gassessment.setgScore(grandSatisfaction.getgScore());
    		gassessment.setgRmk(grandSatisfaction.getgRmk());
			gassessList.add(gassessment);
    		req = EmployeeSatisfactionService.deleteSatisfaction(employee.geteAccount(),grandSatisfaction.geteAccount(),quarter);
    		if(req < 0) {
       		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"满意度测评失败");
    		}
			/*** 改为统一提交
    		req = EmployeeSatisfactionService.insertSatisfaction(gassessment);
    		if(req < 0) {
          		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"满意度测评失败");
       		}****/
        }
		req = EmployeeSatisfactionService.addgassessList(gassessList);
		if(req < 0) {
			reMsg = "评价失败";
			logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
					"出勤考核评价失败");
		}
        //设置前台需要的数据
    	EmployeeSatisfactionVO employeeSatisfactionVO = new EmployeeSatisfactionVO();
    	employeeSatisfactionVO.setCode(0);
    	employeeSatisfactionVO.setMsg(reMsg);
    	employeeSatisfactionVO.setData(listSatisfaction);
        return employeeSatisfactionVO;
    }
    public int checkNumber(Grand grand) {
    	return 0;
    }
}
